import os
import unittest
from common.Log import MyLog as Log
import readConfig as readConfig
from common import HTMLTestRunner_for_py3
from common import SendMail
from common import globalvar as gl
import readAllCase as readAllCase

from common.configEmail import MyEmail






localReadConfig = readConfig.ReadConfig()


class AllTest:
    def __init__(self):
        global log, logger, resultPath, on_off
        gl._init()#初始化全局变量
        log = Log.get_log()
        logger = log.get_logger()
        gl.set_value('logger', logger)#传递logger
        resultPath = log.get_report_path()
        on_off = localReadConfig.get_email("on_off")
        self.caseListFile = os.path.join(readConfig.proDir, "caselist.txt")
        #self.caseFile = os.path.join(readConfig.proDir, "testCase")
        self.caseFile ="/Users/qts/Downloads/interfaceTest/testCase/user/"

        # self.caseFile = None
        self.caseList = []
        self.email = MyEmail.get_email()

    def set_case_list(self):
        """
        set case list
        :return:
        """
        fb = open(self.caseListFile)
        for value in fb.readlines():
            data = str(value)
            if data != '' and not data.startswith("#"):
                self.caseList.append(data.replace("\n", ""))
        fb.close()

    def set_case_suite(self):
        """
        set case suite
        :return:
        """
        self.set_case_list()
        test_suite = unittest.TestSuite()

        suite_module = []

        for case in self.caseList:
            case_name = case.split("/")[-1]
            print(case_name+".py"+"断点")

           #discover = unittest.defaultTestLoader.discover(self.caseFile, pattern='testLogin.py', top_level_dir=None)
            #批量跑
            discover = unittest.defaultTestLoader.discover(str(readAllCase.path), pattern='test*.py')

            suite_module.append(discover)
            print("suite_module:"+str(suite_module))

        if len(suite_module) > 0:

            for suite in suite_module:
                for test_name in suite:
                    test_suite.addTest(test_name)
                    #print("test_suite:"+test_name)
        else:
            return None

        return test_suite

    def run(self):
        """
        run test
        :return:
        """
        try:
            suit = self.set_case_suite()
            print("suit:"+str(suit))
            if suit is not None:
                logger.info("********TEST START********")
                fp = open(resultPath, 'wb')
                print("pf:"+str(fp))
                print('resultPath:'+resultPath)
                print("suit:")
                runner = HTMLTestRunner_for_py3.HTMLTestRunner(stream=fp, title='Test Report', description='')
                runner.run(suit)


            else:
                logger.info("Have no case to test.")
        except Exception as ex:
                logger.error(str(ex))
        finally:
            logger.info("*********TEST END*********")
            fp.close()
            #new_report = SendMail.new_report(resultPath)
            SendMail.send_file(resultPath)
            # send test report by email
            if on_off == 'on':
                self.email.send_email()
            elif on_off == 'off':
                logger.info("Doesn't send report email to developer.")
            else:
                logger.info("Unknow state.")


if __name__ == '__main__':
    obj = AllTest()
    obj.run()
    print("caseFile"+str(obj.caseFile))
